{extend name="sitehome@style/base"/}
{block name="resources"}
<style>
	.profession-box .layui-elem-quote, .niu-side-right-box .layui-elem-quote{margin-left: 0; margin-right: 0;}
	.layui-elem-quote{margin-bottom: 20px;margin-left: 20px;margin-right: 20px;color: #999;}
</style>
{/block}
{block name="main"}

<blockquote class="layui-elem-quote">
	<p><i class="layui-icon layui-icon-tips"></i>  会员组的总成长值是根据(成长值+贡献)的值算出来的,管理员不能直接修改会员所在的会员组. 如果需要修改会员组,请通过设置成长值或者贡献的值来影响总成长值,系统会根据影响后的总成长值自动算出对应的会员组</p>
	<p><i class="layui-icon layui-icon-tips"></i>  系统会根据会员的总成长值(成长值+贡献)多少自动对会员的分组进行调整</p>
</blockquote>

<div class="nc-function-search">
	<button class="layui-btn" onclick="openModel()">添加会员组</button>
	<button class="layui-btn" onclick="openConfigModel()">会员组变更配置</button>
</div>
<div class="nc-table-box">
	<table id="group_list" lay-filter="member_group"></table>
</div>

<script type="text/html" id="create_time">
	<span>{{nc.time_to_date(d.create_time)}}</span>
</script>

<script type="text/html" id="operation">
	<a class="default" lay-event="edit">编辑</a> |
	<a class="default" lay-event="delete">删除</a> <br/>
</script>

<script type="text/html" id="batchOperation">
	<button class="layui-btn layui-btn-primary" lay-event="delete">删除</button>
</script>

{/block}
{block name="script"}
<script type="text/html" id="group_html">
	<div class="layui-form member-form" id="group_block" lay-filter="form">
		<div class="layui-form-item">
			<label class="layui-form-label"><span class="required">*</span>会员组名称</label>
			<div class="layui-input-inline">
				<input type="text" name="group_name"  value="{{ d.group_name || '' }}" required lay-verify="required" autocomplete="off" class="layui-input nc-len-mid" maxlength="16">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">所需成长值</label>
			<div class="layui-input-inline">
				<input type="number" name="credit"  value="{{ d.credit || 0 }}" required lay-verify="credit" placeholder="请输入所需成长值" autocomplete="off" class="layui-input nc-len-tiny" min="0" >
			</div>
		</div>
		<div class="nc-form-row">
			<input type="hidden" name="group_id" value="{{ d.group_id || 0 }}"/>
			<input type="button" value="保存" class="layui-btn"  lay-submit="" lay-filter="group_submit"/>
			<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
		</div>
	</div>
</script>

<script type="text/html" id="group_config_html">
	<div class="layui-form member-group-form" id="group_config_block" lay-filter="form">
		<div class="layui-form-item">
			<label class="layui-form-label sm"></label>
			<div class="layui-input-block">
				<input type="radio" name="group_config" value="1" title="不自动变更"{{#  if(d.type == 1 ){ }} checked {{#  } }}>
			</div>
			<p class="layui-form-mid layui-word-aux ">会员组的变更只能通过管理员来变更。</p>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label sm"></label>
			<div class="layui-input-block">
				<input type="radio" name="group_config" value="2" title="根据成长值多少自动升降"{{#  if(d.type == 2 ){ }} checked {{#  } }}>
			</div>
			<p class="layui-form-mid layui-word-aux">系统根据当前会员的总成长值，按照每个会员组所需总成长值的设置进行变更。可自动升降</p>

		</div>
		<div class="layui-form-item">
			<label class="layui-form-label sm"></label>
			<div class="layui-input-block">
				<input type="radio" name="group_config" value="3" title="根据成长值多少只升不降"{{#  if(d.type == 3 ){ }} checked {{#  } }}>
			</div>
			<p class="layui-form-mid layui-word-aux">系统根据当前会员的总成长值，如果会员的总成长值达到更高一级的会员组，则变更会员组，如果成长值少于当前所在会员组所需总成长值，保持当前会员组不变，不会降级。</p>
		</div>
		<div class="nc-form-row sm">
			<input type="button" value="保存" class="layui-btn"  lay-submit="" lay-filter="group_config_submit"/>
			<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
		</div>
	</div>
</script>
<script>
	var table,form,laytpl;
	$(function () {
		
		table = new Table({
			elem : '#group_list',
			filter: "member_group",
			url : '{:addon_url("applet2://sitehome/group/grouplist")}',
			cols : [[
				{type : 'checkbox',unresize : 'true'},
				{field : 'group_name', width : '30%', title : '会员组名称',unresize : 'true'},
				{field : 'credit', width : '15%', title : '所需成长值',unresize : 'true'},
				{field : 'create_time', width : '40%', title : '创建时间', templet: '#create_time',unresize : 'true'},
				{title : '操作', toolbar : '#operation',align:'right',unresize : 'true'}
			]],
			bottomToolbar: "#batchOperation"
		});
		
		//批量操作
		table.bottomToolbar(function(obj){
			if(obj.data.length < 1){
				layer.msg('请选择要操作的数据');
				return;
			}
			switch(obj.event){
				case "delete":
					var id_array = new Array();
					for(i in obj.data) id_array.push(obj.data[i].group_id);
					deleteLevel(id_array.toString());
					break;
			}
			
		});
		
		//监听工具条
		table.tool(function(obj){
			var data = obj.data;
			switch (obj.event) {
				case 'edit':
					openModel(data);
					break;
				case 'delete':
					deleteLevel(data.group_id);
					break;
			}
		});
		
		layui.use(['laytpl', 'form'], function(){
			laytpl = layui.laytpl;
			form = layui.form;
			var repeat_flag = false;//防重复标识
			form.verify({
				credit: function (value, item) {
					if (value == '') {
						return '请输入所需成长值';
					}
					if (value != '' && /\D/.test(value)) {
						return "所需成长值请输入正整数";
					}
				},
			});
			
			form.on('submit(group_submit)', function(data){
				if(repeat_flag) return;
				repeat_flag = true;
				var field = data.field;
				$.ajax({
					type:"post",
					url : '{:addon_url("applet2://sitehome/group/addGroup")}',
					data: field,
					dataType : "JSON",
					success:function(res){
						layer.msg(res.message);
						if(res.code == 0){
							layer.close(layer_index);
							table.reload();
						}
						repeat_flag = false;
					}
				});
				
				return false;
			});
			
			form.on('submit(group_config_submit)', function(data){
				if(repeat_flag) return;
				repeat_flag = true;
				$.ajax({
					type:"post",
					url : '{:addon_url("applet2://sitehome/group/setMemberGroupConfig")}',
					data: data.field,
					dataType : "JSON",
					success:function(res){
						layer.msg(res.message);
						if(res.code == 0){
							layer.close(layer_index);
						}
						repeat_flag = false;
					}
				});
			});
		});
	});
	
	/**
	 * 删除会员组
	 */
	function deleteLevel(group_id) {
		layer.confirm('确定删除吗?', {
			btn: ['确定','取消']
		},  function (index) {
			$.ajax({
				type:"post",
				async:false,
				url : '{:addon_url("applet2://sitehome/group/deleteGroup")}',
				data: {'group_id' : group_id},
				dataType: 'json',
				success:function(res){
					layer.msg(res.message);
					if(res.code == 0){
						table.reload();
					}
				}
			});
		}, function(){
			layer.close();
		});
	}
	
	/**
	 * 添加或编辑弹窗
	 */
	function openModel(data){
		if(data == undefined){
			title = "添加会员组";
			data = {};
		}else{
			title = "修改会员组";
		}
		var tpl_html = $("#group_html").html();
		laytpl(tpl_html).render(data, function(html){
			layer_index = layer.open({
				title: title,
				type: 1,
				area: ['550px'],
				content: html,
			});
		});
		form.render();//重载form表单
	}
	
	/**
	 * 分组变更配置弹框
	 */
	function openConfigModel(){
		var data = [];
		$.ajax({
			type:"post",
			url : '{:addon_url("applet2://sitehome/group/getMemberGroupConfig")}',
			dataType: 'json',
			async : false,
			success:function(res){
				if(res.code == 0){
					data = res.data.value;
				}
			}
		});
		
		var tpl_html = $("#group_config_html").html();
		laytpl(tpl_html).render(data, function(html){
			layer_index = layer.open({
				title: "会员组变更配置",
				type: 1,
				area: ['850px','450px'],
				content: html,
			});
		});
		form.render();//重载form表单
	}
</script>
{/block}